<?php

/**
 * @author 
 * @copyright 2009
 */

class TemplateParser{
	
	var $arrFileContent;
	var $txtLoopOutput;
	var $intFileContentCount;
	var $intRemember;
	var $arrFill;
	
	function loadfile($objFile)
	{
		$this->arrFileContent = file($objFile);
		if (!is_array($this->arrFileContent))
		{
			echo "<p class=\"error\">Error while loading file ".$objFile."</p>";
		}
	}
	function assignVars($arrVariables)
	{
		if (is_array($arrVariables))
		{
			foreach($arrVariables as $key => $value)
			{
				$key = "{".$key."}";
				if (is_array($value))
				{
					for($i=0;$i<=count($this->arrFileContent);$i++)
					{
						if (array_key_exists($i, $this->arrFileContent))
						{
							if (strpos($this->arrFileContent[$i], $key) === false)
							{
								$arrTemp[] = $this->arrFileContent[$i];
							}
							else
							{
								$arrTemp = array_merge($arrTemp, $value);
							}
						}
					}
					$this->arrFileContent = $arrTemp;
					unset($arrTemp);
				}
				else
				{
					$this->arrFileContent = str_replace($key, mb_convert_encoding($value,"UTF-8"), $this->arrFileContent);
				}
			}
		}
	}
	function runLoop($txtLoopName, $arrVars)
	{
		
		$intCount = count($arrVars);
		foreach($this->arrFileContent as $key => $value)
		{
			if (strpos($this->arrFileContent[$key], "<!-- Begin Loop $txtLoopName -->") !== false)
			{
				$intLoopBegin = $key + 1;
			}
			if (strpos($this->arrFileContent[$key], "<!-- End Loop $txtLoopName -->")  !== false)
			{
				$intLoopEnd = $key - 1;
			}
		}
		for ($i=$intLoopBegin;$i<=$intLoopEnd;$i++)
		{
			$arrLoop[] = $this->arrFileContent[$i];
			unset($this->arrFileContent[$i]);
		}
		
		foreach ($arrVars[0] as $key => $value)
		{
			$arrLoopNames[] = $key;
		}
		
		$i = 0;
		$j = 0;
		foreach ($this->arrFileContent as $key => $value)
		{
			if ($i<$intLoopBegin)
			{
				$this->arrPart1[$i] = $value;
				$i++;
			}
			else
			{
				$this->arrPart2[$j] = $value;
				$j++;
			}
		}
		
		unset($this->arrPart1[$i - 1]);
		unset($this->arrPart2[0]);
		
		
		for ($i=0;$i<$intCount;$i++)
		{
			foreach ($arrLoop as $key => $value)
			{
				foreach ($arrLoopNames as $key2 => $value2)
				{
					$value = str_replace("{".$value2."}", $arrVars[$i][$value2], $value);
				}
				$this->arrPart3[] = $value;
			}
		}
		

		$this->arrFileContent = array_merge($this->arrPart1, $this->arrPart3, $this->arrPart2);
		unset($this->arrPart1);
		unset($this->arrPart2);
		unset($this->arrPart3);
	}
	
	// Flush The Output
	function flushOutput($intLines=-1)
	{	
		if ($intLines == -1) $intLines = count ($this->arrFileContent);
		
		$i = 0;
		
		foreach ($this->arrFileContent as $key => $value)
		{
			
			if ($i>$intLines)
			{
				break;
			}
			echo $value;
			unset($this->arrFileContent[$key]);
			$i++;
		}
	}
	
	function output()
	{
		foreach($this->arrFileContent as $key => $value)
		{
			echo $value;
		}
	}
	
	function result()
	{
		$result = "";
		foreach($this->arrFileContent as $key => $value)
		{
			$result .= $value;
		}
		return $result;
	}
	
	function dump()
	{
		foreach($this->arrFileContent as $key => $value)
		{
			echo $key." ".htmlspecialchars($value);
		}	
	}

}

?>